package com.acce.gxsc.page.outCar.feeDetail;

import android.graphics.Bitmap;
import android.view.View;

import com.acce.gxsc.common.App;
import com.acce.gxsc.common.Tools;
import com.acce.gxsc.model.WorkModel;
import com.acce.gxsc.model.loaclBean.LocalFeeCarDetailBean;
import com.acce.gxsc.model.loaclBean.ResultBean;
import com.acce.sdk.base.mvp.BasePresenter;
import com.acce.sdk.base.volleyHttp.GsonUtils;
import com.acce.sdk.utils.LogUtils;
import com.acce.sdk.utils.ToastUtils;
import com.sunmi.impl.V1Printer;
import com.sunmi.util.ThreadPoolManager;

import rx.Subscriber;

import static com.acce.gxsc.common.Constants.OUT_CAR_GATE_NO;

/**
 * Created by huxley on 2017/5/26.
 */

public class ToOutCarDetailPresenter extends BasePresenter<ToOutCarDetailView> {

    private V1Printer printer;
    private Bitmap    orderBitmap;

    void printOrder(final View orderView) {
        if (mViewRef != null) {
            if (orderBitmap == null) {
                orderBitmap = Tools.convertViewToBitmap(orderView, 0.85f);
            }
            orderView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    try {
                        ThreadPoolManager.getInstance().executeTask(new Runnable() {
                            @Override
                            public void run() {
                                if (printer == null) {
                                    printer = new V1Printer(App.context);
                                }
                                printer.beginTransaction();
                                printer.setAlignment(1);
                                printer.printBitmap(orderBitmap);
                                printer.lineWrap(4);
                                printer.printHorizontalLine(1, 0);
                                printer.lineWrap(2);
                                printer.commitTransaction();
                                try {
                                    Thread.sleep(1000);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                                if (mViewRef != null) {
                                    orderView.postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            getView().printSuccess();
                                        }
                                    }, 1500);
                                }
                            }
                        });
                    } catch (Exception e) {
                        ToastUtils.show("打印失败！");
                    }
                }
            }, 300);
        }
    }

    public void loadInCarDetail(String inCarKey) {
        WorkModel.getInstance().getFeeCarDetail(inCarKey).subscribe(new Subscriber<ResultBean<LocalFeeCarDetailBean>>() {
            @Override
            public void onStart() {
                if (mViewRef != null) {
                    getView().showProgressDialog("加载中...", false);
                }
            }
            @Override
            public void onNext(ResultBean<LocalFeeCarDetailBean> datas) {
                if (mViewRef != null) {
                    getView().loadSuccess(datas.result);
                }
            }
            @Override
            public void onCompleted() {
                if (mViewRef != null) {
                    getView().hideProgressDialog();
                }
            }
            @Override
            public void onError(Throwable e) {
                if (mViewRef != null) {
                    e.printStackTrace();
                    getView().hideProgressDialog();
                }
            }
        });
    }


    public void toOutCar(String inCarKey, String outCarRemarks) {
        if (!Tools.verificationId(OUT_CAR_GATE_NO)) {
            return;
        }
        WorkModel.getInstance().toOutCar(inCarKey, outCarRemarks).subscribe(new Subscriber<ResultBean<Object>>() {
            @Override
            public void onStart() {
                if (mViewRef != null) {
                    getView().showProgressDialog("离场中...", false);
                }
            }
            @Override
            public void onNext(ResultBean<Object> datas) {
                if (mViewRef != null) {
                    LogUtils.logout(GsonUtils.toJson(datas));
                    ToastUtils.show(datas.returnMsg);
                    if ("200".equals(datas.returnCode)) {
                        getView().toOutCarSuccess();
                    }
                }
            }
            @Override
            public void onCompleted() {
                if (mViewRef != null) {
                    getView().hideProgressDialog();
                }
            }
            @Override
            public void onError(Throwable e) {
                if (mViewRef != null) {
                    e.printStackTrace();
                    ToastUtils.show("删除失败！");
                    getView().hideProgressDialog();
                }
            }
        });
    }
}
